<template>
  <div class="app-container main-box">
    <el-card class="border-none search-card" shadow="never">
      <el-form :model="queryParams" class="search-form" ref="queryRef" :inline="true" v-show="showSearch"
        label-width="68px">

        <sh-search ref="search" :model="form" :resetBtn="true">
        
          <el-form-item label="项目代码" prop="subjectCode">
            <el-input v-model="queryParams.subjectCode" placeholder="请输入项目代码" clearable @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="更新时间">
            <el-date-picker v-model="daterangeUpdateTime" style="width: 240px" value-format="YYYY-MM-DD" type="daterange"
              range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
          </el-form-item>

          <template v-slot:btn>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
             </template>

       </sh-search>
      </el-form>
    </el-card>
    <el-card class="border-none table-card" shadow="never">
      <el-row :gutter="10" class="mb8 action-btns">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="Plus" @click="handleAdd"
            v-hasPermi="['sup:managerSubject:add']">新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
            v-hasPermi="['sup:managerSubject:edit']">修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
            v-hasPermi="['sup:managerSubject:remove']">删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="warning" plain icon="Download" @click="handleExport"
            v-hasPermi="['sup:managerSubject:export']">导出</el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" border :data="managerSubjectList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="项目ID" align="center" prop="id" />
        <el-table-column label="项目代码" align="center" prop="subjectCode" />
        <el-table-column label="项目名称" align="center" prop="subjectName" />
        <el-table-column label="项目类型" align="center" prop="subjectGenus">
          <template #default="scope">
            <dict-tag :options="subject_genus" :value="scope.row.subjectGenus" />
          </template>
        </el-table-column>
        <el-table-column label="建设性质" align="center" prop="buildType">
          <template #default="scope">
            <dict-tag :options="build_type" :value="scope.row.buildType" />
          </template>
        </el-table-column>
        <el-table-column label="计划开工日期" align="center" prop="startDate" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.startDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="计划竣工日期" align="center" prop="endDate" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column  label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button type="text" icon="Edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['sup:managerSubject:edit']">修改</el-button>
            <el-button type="text" icon="Delete" @click="handleDelete(scope.row)"
              v-hasPermi="['sup:managerSubject:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
        @pagination="getList" />
    </el-card>
    <!-- 添加或修改项目信息对话框 -->
    <el-dialog :title="title" v-model="open" width="1200px" append-to-body :fullscreen="dialogFullScreen"
      :class="[dialogFullScreen ? 'fullscreen' : 'no_fullscreen']">
      <template #title>
        <div class="custom_dialog_header">
          <span class="el-dialog__title">{{ title }}</span>
          <div class="custom_dialog_menu" @click="dialogFullScreen = !dialogFullScreen">
            <svg-icon class="fullscreen-icon" :icon-class="dialogFullScreen ? 'fullscreen-exit' : 'fullscreen'" />
          </div>
        </div>
      </template>
      <el-form ref="managerSubjectRef" :model="form" :rules="rules" label-width="auto">
        <el-row>
          <el-col :span="12">
            <el-form-item label="项目代码" prop="subjectCode">
              <el-input v-model="form.subjectCode" placeholder="请输入项目代码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目名称" prop="subjectName">
              <el-input v-model="form.subjectName"  placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="项目类型" prop="subjectGenus">
              <el-select v-model="form.subjectGenus" clearable filterable placeholder="请选择项目类型">
                <el-option v-for="dict in subject_genus" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="建设性质" prop="buildType">
              <el-select v-model="form.buildType" clearable filterable placeholder="请选择建设性质">
                <el-option v-for="dict in build_type" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="所在省" prop="provinceCode">
              <el-input v-model="form.provinceCode" placeholder="请输入所在省代码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所在市" prop="cityCode">
              <el-input v-model="form.cityCode" placeholder="请输入所在市代码" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="所在区" prop="areaCode">
              <el-input v-model="form.areaCode" placeholder="请输入所在区代码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="街镇" prop="townCode">
              <el-input v-model="form.townCode" placeholder="请输入街镇" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="立项批文(证书编号)" prop="licence">
              <el-input v-model="form.licence" placeholder="请输入立项批文(证书编号)" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="投资预算" prop="amount">
              <el-input v-model="form.amount" placeholder="请输入投资预算" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="批复机关" prop="approvalDepart">
              <el-input v-model="form.approvalDepart" placeholder="请输入立项批复机关" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="批复时间" prop="approvalDate">
              <el-date-picker clearable v-model="form.approvalDate" type="date" value-format="YYYY-MM-DD"
                placeholder="选择立项批复时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="建设地点" prop="address">
              <el-input v-model="form.address" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="计划开工日期" prop="startDate">
              <el-date-picker clearable v-model="form.startDate" type="date" value-format="YYYY-MM-DD"
                placeholder="选择计划开工日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="计划竣工日期" prop="endDate">
              <el-date-picker clearable v-model="form.endDate" type="date" value-format="YYYY-MM-DD"
                placeholder="选择计划竣工日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="ManagerSubject">
import { listManagerSubject, getManagerSubject, delManagerSubject, addManagerSubject, updateManagerSubject } from "@/api/sup/managerSubject";








const { proxy } = getCurrentInstance();
const { subject_genus, build_type } = proxy.useDict('subject_genus', 'build_type');

const managerSubjectList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
let router = useRouter();
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dialogFullScreen = ref(false);
const daterangeApprovalDate = ref([]);
const daterangeStartDate = ref([]);
const daterangeEndDate = ref([]);
const daterangeCreateTime = ref([]);
const daterangeUpdateTime = ref([]);



const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    subjectCode: null,
    subjectName: null,
    updateTime: null,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询项目信息列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  if (null != daterangeApprovalDate && '' != daterangeApprovalDate) {
    queryParams.value.params["beginApprovalDate"] = daterangeApprovalDate.value[0];
    queryParams.value.params["endApprovalDate"] = daterangeApprovalDate.value[1];
  }
  if (null != daterangeStartDate && '' != daterangeStartDate) {
    queryParams.value.params["beginStartDate"] = daterangeStartDate.value[0];
    queryParams.value.params["endStartDate"] = daterangeStartDate.value[1];
  }
  if (null != daterangeEndDate && '' != daterangeEndDate) {
    queryParams.value.params["beginEndDate"] = daterangeEndDate.value[0];
    queryParams.value.params["endEndDate"] = daterangeEndDate.value[1];
  }
  if (null != daterangeCreateTime && '' != daterangeCreateTime) {
    queryParams.value.params["beginCreateTime"] = daterangeCreateTime.value[0];
    queryParams.value.params["endCreateTime"] = daterangeCreateTime.value[1];
  }
  if (null != daterangeUpdateTime && '' != daterangeUpdateTime) {
    queryParams.value.params["beginUpdateTime"] = daterangeUpdateTime.value[0];
    queryParams.value.params["endUpdateTime"] = daterangeUpdateTime.value[1];
  }
  listManagerSubject(queryParams.value).then(response => {
    managerSubjectList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    subjectCode: null,
    subjectName: null,
    subjectGenus: null,
    buildType: null,
    provinceCode: null,
    cityCode: null,
    areaCode: null,
    townCode: null,
    licence: null,
    amount: null,
    approvalDepart: null,
    approvalDate: null,
    address: null,
    startDate: null,
    endDate: null,
    createTime: null,
    updatorId: null,
    updatorName: null,
    updateTime: null,
    invalid: null,
    origin: null
  };
  proxy.resetForm("managerSubjectRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeApprovalDate.value = [];
  daterangeStartDate.value = [];
  daterangeEndDate.value = [];
  daterangeCreateTime.value = [];
  daterangeUpdateTime.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加项目信息";

}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const id = row.id || ids.value
  getManagerSubject(id).then(response => {
    form.value = response.data;

    open.value = true;
    title.value = "修改项目信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["managerSubjectRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateManagerSubject(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addManagerSubject(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除项目信息编号为"' + ids + '"的数据项？').then(function () {
    return delManagerSubject(ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('sup/managerSubject/export', {
    ...queryParams.value
  }, `managerSubject_${new Date().getTime()}.xlsx`)
}



onActivated(() => {
  getList();

})

getList();

</script>
